/*
 * CQ5 Quiz Engine
 * A Vink, UPC Nederland 2014
 * Version 1.0
 */

 

jQuery(document).ready(function(){
	//setup
	upcnlQuiz.init();
});

upcnlQuiz = {

	init : function(){
		//config
			//height and width for question icons example ABCD or 1234 styling
			var questionIcons = 32;
	
		quizWidth = 0;
		totalscore = 0;
		totalquestions = 0;
		newcollection = "";
		window["collection"] = [];
		window["collectionsum"] = [];
		compareGet = 0;
		compareSet = "";
		
		jQuery(".nlQuizQuestions .nlQuizQuestion").each(function(_index){
			quizWidth += jQuery(this).outerWidth();
			jQuery(this).data("index", _index);
		});

		jQuery(".nlQuizQuestions .nlQuizQuestion").each(function(){
			jQuery(this).appendTo(jQuery("#nlQuiz"));
		});
				
		jQuery("#nlQuiz .nlQuizQuestion").wrapAll("<div class='wrapper' />");
		jQuery("#nlQuiz .wrapper").css({ "width" : quizWidth })
		
		
		jQuery("#nlQuiz .nlQuizQuestion").css({"width" : jQuery("#nlQuiz").outerWidth() - 40}); //40 = padding
		jQuery("#nlQuiz .nlQuizQuestion:last-child").css({"width" : jQuery("#nlQuiz").outerWidth() - 51});
		
		jQuery( window ).resize(function() {
			//jQuery("#nlQuiz .wrapper").css({ marginLeft : parseFloat("-" + jQuery("#nlQuiz .nlQuizQuestion").outerWidth() * (jQuery(".answer.selected").length + 1)) - 5 });
			jQuery("#nlQuiz .nlQuizQuestion").css({"width" : jQuery("#nlQuiz").outerWidth() - 40});
			jQuery("#nlQuiz .nlQuizQuestion:last-child").css({"width" : jQuery("#nlQuiz").outerWidth() - 51});
		});
		
		var startScreen = (jQuery(".nlQuizQuestion.start").length > 0) ? 1 : 0;
		jQuery(".nlQuizQuestion .total").text(jQuery("#nlQuiz .nlQuizQuestion").length - (jQuery("#nlQuiz .nlQuizQuestion.result").length + startScreen));
		jQuery(".nlQuizQuestion").each(function(key, value){
			if(jQuery(this).hasClass("result")){
				//
			}else{
				jQuery(this).find(".number").text(key);
			}
		});
		
		
		jQuery(".nlQuizQuestion ul").each(function(){
			var i = 0;
			var j = 0;
			jQuery(jQuery(this).find("li")).each(function(){
				jQuery(this).prepend("<div class='icon' />");
				i++;
				jQuery(this).find(".icon").css({"background-position" : "0px -" + j.toString() +"px"});
				
				jQuery(this).find(".answer").mouseenter(function(e){				
					var style = jQuery(jQuery(e.currentTarget).parent().find(".icon")).css("background-position");
						style = style.split(" ")[0].replace("0px", questionIcons+"px") + " " + style.split(" ")[1];
					jQuery(e.currentTarget).parent().find(".icon").css({"background-position" : style});
				});
				jQuery(this).find(".answer").mouseleave(function(e){
					var style = jQuery(jQuery(e.currentTarget).parent().find(".icon")).css("background-position");
						style = style.split(" ")[0].replace(questionIcons+"px", "0px") + " " + style.split(" ")[1];
						jQuery(e.currentTarget).parent().find(".icon").css({"background-position" : style});
				});
				j = i*questionIcons;
			});
		});
		
		//flip start screen
		try{
			jQuery(jQuery(".nlQuizQuestion.start > div").get(0)).before(jQuery(".nlQuizQuestion.start > div").get(1));
		}catch(err){
		}
		
		this.next();
	},
	
	next : function(){
		jQuery("#nlQuiz .nlQuizQuestion .answer").click(function(){
			jQuery("#nlQuiz .wrapper").animate({ marginLeft : parseFloat("-" + jQuery("#nlQuiz .nlQuizQuestion").outerWidth() * (jQuery(".answer.selected").length + 1)) - 5 }, 1000, "easeInOutCubic");
				jQuery(this).addClass("selected");
				jQuery(this).attr("data-array", jQuery(this).data("genre") + "_array");		
				upcnlQuiz.calculation(jQuery(this).data("value"), jQuery(this).parents(".nlQuizQuestion").data("index"));
		});
	},
	
	prev : function(){
		jQuery("#nlQuiz .wrapper").animate({ marginLeft : parseFloat("-" + jQuery("#nlQuiz .nlQuizQuestion").outerWidth() * (jQuery(".answer.selected").length - 1)) - 5 }, 1000, "easeInOutCubic");
	},
	
	calculation : function(_value, _index){		
		totalquestions = jQuery("#nlQuiz .nlQuizQuestion").length - (jQuery("#nlQuiz .nlQuizQuestion.result").length + 1);	
		jQuery(".answer.selected").each(function(){
			groupArray(jQuery(this).data("genre") + "_array", jQuery(this));
			if(newcollection != jQuery(this).data("genre")){
				window["collection"].push(jQuery(this).data("genre"));
				newcollection = jQuery(this).data("genre");
			}
		});
		
		
		//last slide
 		if(_index == totalquestions){		
			for(i=0; i<window["collection"].length; i++){
				window["collectionsum"][i] = 0;
				jQuery(".answer.selected").each(function(){
					if(jQuery(this).attr("data-genre") == window["collection"][i]){
						window["collectionsum"][i] += jQuery(this).data("value");
						jQuery(".answer.selected[data-genre='"+window["collection"][i]+"']").attr("data-sum", window["collectionsum"][i]);
						jQuery(".answer.selected").data("sum");
					}
					if(jQuery(this).data("sum") > compareGet){
						compareSet = jQuery(this);
						compareGet = jQuery(this).data("sum");
					}
				});
			}
			
			if(location.search.indexOf("em=") > -1){
				jQuery(".form input[type='text']:visible").val(location.search.split("em=")[1]);
			}
		}
		
		totalscore += parseFloat(_value);
		
		function groupArray(_name, _this){
			window[_name] = jQuery.makeArray(jQuery(".answer.selected[data-genre='"+_this.data("genre")+"']"));
		}
		
		//render score
		jQuery(".totalscore").remove();
		if(_index == totalquestions){	
			jQuery(".nlQuizQuestion.result").each(function(){
				if(jQuery(this).hasClass(compareSet.data("genre"))){
					jQuery(this).show();
					jQuery(".selectType img:visible").each(function(){
						if(jQuery(this).attr("src").indexOf(compareSet.data("genre")) > -1){
							jQuery(this).attr("src", jQuery(this).attr("src").replace(".png", "-active.png")).css({"opacity" : "1"});
						}
					});
				}else{
					jQuery(this).hide();
				}
			});
			

			//pixelpost
			jQuery(".splitbox .form:visible .upc_button8").unbind().click(function(){
			
				if(jQuery(".splitbox .form:visible .checkbox").is(":checked")){
					jQuery(".splitbox .form:visible .pixel").remove();
					jQuery(".splitbox .form:visible")
						.append("<img class='pixel' src='http://upc.cm-apps.co.uk/PixelPost/Register.ashx?kn=&em="+jQuery(".splitbox .form:visible input").val()+"&ti=&in=&md=&ln=&pc=&hn=&ne=&nb=1&wc=tvquiz_"+compareSet.data("genre")+"&bd=&te=' />");
					jQuery(".splitbox .form:visible").html("<p style='padding: 10px; margin: 0px;'>Bedankt voor uw aanmelding. Houd uw e-mail in de gaten voor persoonlijke kijktips.</p>");
				}else{
					jQuery(".splitbox .form:visible .checkbox").next().fadeOut(200).fadeIn(200).css({"color" : "red"});
				}

			});

		}
	}

}